<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/blog/","scheme":"Muse","version":"8.0.0-rc.4","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="JSP注释JSP注释的内容不会出现在客户端的的页面源码中 1&lt;%-- 注释内容 --%&gt; JSP指令1.page指令页面指令, JSP页面全局指令, 会作用于整个JSP页面共13条属性 1&lt;%@ page 属性&#x3D;&quot;值&quot; 属性&#x3D;&quot;值&quot; %&gt;">
<meta property="og:type" content="article">
<meta property="og:title" content="JSP语法">
<meta property="og:url" content="https://gitee.com/indoorsman_lab/blog/2020/07/17/JSP%E8%AF%AD%E6%B3%95/index.html">
<meta property="og:site_name" content="宅人类研究室">
<meta property="og:description" content="JSP注释JSP注释的内容不会出现在客户端的的页面源码中 1&lt;%-- 注释内容 --%&gt; JSP指令1.page指令页面指令, JSP页面全局指令, 会作用于整个JSP页面共13条属性 1&lt;%@ page 属性&#x3D;&quot;值&quot; 属性&#x3D;&quot;值&quot; %&gt;">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-07-17T06:26:51.000Z">
<meta property="article:modified_time" content="2020-07-17T06:35:06.712Z">
<meta property="article:author" content="张雪峰">
<meta property="article:tag" content="JSP">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://gitee.com/indoorsman_lab/blog/2020/07/17/JSP%E8%AF%AD%E6%B3%95/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>JSP语法 | 宅人类研究室</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line toggle-line-first"></span>
        <span class="toggle-line toggle-line-middle"></span>
        <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">宅人类研究室</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">IndoorsMan Lab</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP注释"><span class="nav-number">1.</span> <span class="nav-text">JSP注释
</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP指令"><span class="nav-number">2.</span> <span class="nav-text">JSP指令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-page指令"><span class="nav-number">3.</span> <span class="nav-text">1.page指令
</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-include指令"><span class="nav-number">4.</span> <span class="nav-text">2.include指令
</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-taglib指令"><span class="nav-number">5.</span> <span class="nav-text">3.taglib指令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP脚本元素"><span class="nav-number">6.</span> <span class="nav-text">JSP脚本元素</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-JSP声明语句"><span class="nav-number">7.</span> <span class="nav-text">1.JSP声明语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-JSP表达式"><span class="nav-number">8.</span> <span class="nav-text">2.JSP表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-JSP-Scriptlet"><span class="nav-number">9.</span> <span class="nav-text">3.JSP Scriptlet</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP动作元素"><span class="nav-number">10.</span> <span class="nav-text">JSP动作元素</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-lt-jsp-include-gt"><span class="nav-number">11.</span> <span class="nav-text">1.&lt;jsp:include&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-lt-jsp-forward-gt"><span class="nav-number">12.</span> <span class="nav-text">2.&lt;jsp:forward&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-lt-jsp-param-gt"><span class="nav-number">13.</span> <span class="nav-text">3.&lt;jsp:param&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-lt-jsp-plugin-gt"><span class="nav-number">14.</span> <span class="nav-text">4.&lt;jsp:plugin&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-lt-jsp-useBean-gt"><span class="nav-number">15.</span> <span class="nav-text">5.&lt;jsp:useBean&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-lt-jsp-setProperty-gt"><span class="nav-number">16.</span> <span class="nav-text">6.&lt;jsp:setProperty&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-lt-jsp-getProperty-gt"><span class="nav-number">17.</span> <span class="nav-text">7.&lt;jsp:getProperty&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP内置对象"><span class="nav-number">18.</span> <span class="nav-text">JSP内置对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-request"><span class="nav-number">19.</span> <span class="nav-text">1.request</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-response对象"><span class="nav-number">20.</span> <span class="nav-text">2.response对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-out对象"><span class="nav-number">21.</span> <span class="nav-text">3.out对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-session对象"><span class="nav-number">22.</span> <span class="nav-text">4.session对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-application对象"><span class="nav-number">23.</span> <span class="nav-text">5.application对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-pageContext对象"><span class="nav-number">24.</span> <span class="nav-text">6.pageContext对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-page对象"><span class="nav-number">25.</span> <span class="nav-text">7.page对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-config对象"><span class="nav-number">26.</span> <span class="nav-text">8.config对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-exception对象"><span class="nav-number">27.</span> <span class="nav-text">9.exception对象</span></a></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="张雪峰"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">张雪峰</p>
  <div class="site-description" itemprop="description">IndoorsMan Lab</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">15</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/indoorsmanlab" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;indoorsmanlab" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/indoorsman_lab" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;indoorsman_lab"><i class="fab fa-git-square fa-fw"></i>Gitee</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://blog.csdn.net/m0_46238775" title="https:&#x2F;&#x2F;blog.csdn.net&#x2F;m0_46238775" rel="noopener" target="_blank">宅人类研究室-CSDN</a>
        </li>
    </ul>
  </div>

      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/indoorsmanlab" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/indoorsman_lab/blog/2020/07/17/JSP%E8%AF%AD%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="张雪峰">
      <meta itemprop="description" content="IndoorsMan Lab">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="宅人类研究室">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JSP语法
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-07-17 14:26:51 / 修改时间：14:35:06" itemprop="dateCreated datePublished" datetime="2020-07-17T14:26:51+08:00">2020-07-17</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Java-Web/" itemprop="url" rel="index"><span itemprop="name">Java Web</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="JSP注释"><a href="#JSP注释" class="headerlink" title="JSP注释"></a>JSP注释<br></h2><p>JSP注释的内容不会出现在客户端的的页面源码中</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%-- 注释内容 --%&gt;</span><br></pre></td></tr></table></figure>
<h2 id="JSP指令"><a href="#JSP指令" class="headerlink" title="JSP指令"></a>JSP指令</h2><h2 id="1-page指令"><a href="#1-page指令" class="headerlink" title="1.page指令"></a>1.page指令<br></h2><p>页面指令, JSP页面全局指令, 会作用于整个JSP页面<br><br>共13条属性</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page 属性=<span class="string">"值"</span> 属性=<span class="string">"值"</span> %&gt;</span><br></pre></td></tr></table></figure>
<a id="more"></a>

<p>示例:</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;%-- 多个属性值对 --%&gt;</span><br><span class="line">&lt;%@ page contentType=<span class="string">"text/html;charset=ISO-8859-1"</span> <span class="keyword">import</span>=<span class="string">"java.util.*"</span>%&gt;</span><br><span class="line">&lt;%-- 一个属性多个值 --%&gt;</span><br><span class="line">&lt;%@ page <span class="keyword">import</span>=<span class="string">"java.util.*"</span>, <span class="string">"java.io.*"</span>, <span class="string">"java.awt.*"</span> %&gt;</span><br><span class="line">&lt;%-- 多行 --%&gt;</span><br><span class="line">&lt;%@ page contentType=<span class="string">"text/html;charset=ISO-8859-1"</span> %&gt;</span><br><span class="line">&lt;%@ page <span class="keyword">import</span>=<span class="string">"java.util.*"</span> %&gt;</span><br><span class="line">&lt;%@ page <span class="keyword">import</span>=<span class="string">"java.io.*"</span>, <span class="string">"java.awt.*"</span> %&gt;</span><br></pre></td></tr></table></figure>
<h2 id="2-include指令"><a href="#2-include指令" class="headerlink" title="2.include指令"></a>2.include指令<br></h2><p>文件加载指令<br><br>在JSP文件中插入包含文本或代码的文件, 插入后和原来的JSP文件合并成一个新的JSP页面;<br> 如果插入的文件发生了变化, 那包含这个插入文件的JSP需要重新编译<br></p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ include file=<span class="string">"要插入文件的地址"</span> %&gt;</span><br></pre></td></tr></table></figure>
<p>只有一个file属性, 文件地址一般使用相对路径<br></p>
<h2 id="3-taglib指令"><a href="#3-taglib指令" class="headerlink" title="3.taglib指令"></a>3.taglib指令</h2><p>引用标签库, 设置标签库前缀, 允许JSP页面使用用户自定义的标签, 可以为标签库命名</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ taglib uri=<span class="string">"tagLibraryURI"</span> prefix=<span class="string">"tagPrefix"</span> %&gt;</span><br></pre></td></tr></table></figure>
<p>两个属性 uri 和 prefix<br><br>uri指定标签文件或者标签库存放的位置<br><br>prefix指定标签库使用的前缀<br></p>
<h2 id="JSP脚本元素"><a href="#JSP脚本元素" class="headerlink" title="JSP脚本元素"></a>JSP脚本元素</h2><h2 id="1-JSP声明语句"><a href="#1-JSP声明语句" class="headerlink" title="1.JSP声明语句"></a>1.JSP声明语句</h2><p>声明变量和方法</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%! Java中变量或方法的定义 %&gt;</span><br></pre></td></tr></table></figure>
<p>声明的变量会转换成Servlet类中的成员变量(实例属性),<br> 这些变量在整个JSP页面内部都有效, 也叫作 JSP页面的成员标量<br><br>注意: 声明语句的<strong>方法内部定义的变量</strong>相当于是Java中的局部变量, 只在方法内部有效, 伴随方法创建和消亡<br></p>
<h2 id="2-JSP表达式"><a href="#2-JSP表达式" class="headerlink" title="2.JSP表达式"></a>2.JSP表达式</h2><p>表达式必须能求值,<br> 且计算结果会以字符串的形式发送到客户端显示出来</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%= 表达式 %&gt;</span><br></pre></td></tr></table></figure>
<h2 id="3-JSP-Scriptlet"><a href="#3-JSP-Scriptlet" class="headerlink" title="3.JSP Scriptlet"></a>3.JSP Scriptlet</h2><p>Java代码段, 使用Java实现一些复杂的操作或控制时, JSP表达式不能满足要求, 就需要JSP Scriptlet;<br><br>JSP Scriptlet 中声明的变量是JSP页面的局部变量, 伴随JSP Scriptlet创建和消亡;</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;% Java代码 %&gt;</span><br></pre></td></tr></table></figure>

<h2 id="JSP动作元素"><a href="#JSP动作元素" class="headerlink" title="JSP动作元素"></a>JSP动作元素</h2><p>控制JSP的行为, 执行页面动作</p>
<h2 id="1-lt-jsp-include-gt"><a href="#1-lt-jsp-include-gt" class="headerlink" title="1.&lt;jsp:include&gt;"></a>1.&lt;jsp:include&gt;</h2><p>在JSP中包含页面, 静态或动态文件<br></p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:include page=<span class="string">"relative URL"</span> flush=<span class="string">"true|false"</span> /&gt;</span><br></pre></td></tr></table></figure>
<p>两个属性: page 和 flush<br><br>page: 指定被包含文件的URL地址, 相对路径<br><br>flush: 指定当缓冲区满时, 是否将其清空, 默认false不清空<br></p>
<p>动态包含: 当JSP页面运行时才会载入文件, 不是把被包含文件与JSP页面合并成一个新的JSP页面;<br></p>
<p>被包含的文件:<br></p>
<ul>
<li>文本文件, 只需要将内容发送到客户端, 由客户端显示出来<br></li>
<li>JSP文件, JSP编译器先编译该文件, 再将执行结果发送到客户端显示出来</li>
</ul>
<p><strong>&lt;jsp:include&gt;动作元素和include指令的区别</strong><br></p>
<ul>
<li>&lt;jsp:include&gt;动作元素在执行时才对包含文件进行处理, JSP文件和被包含的文件在逻辑和语法上都是独立的, 当被包含文件内容改变时, JSP执行时就能直接看到改变后的结果了, 没什么影响;<br></li>
<li>include指令包含的文件是静态文件, 一旦被包含的文件内容发生变化, JSP页面运行时看到的还是旧的文件内容, 还需要重新编译JSP页面, 然后再次访问才能看到改变后的文件内容<br></li>
</ul>
<p>jsp:include引入txt时会产生中文乱码问题, 最好使用jsp或者静态html作为引入文件, 避免使用txt文本.<br></p>
<h2 id="2-lt-jsp-forward-gt"><a href="#2-lt-jsp-forward-gt" class="headerlink" title="2.&lt;jsp:forward&gt;"></a>2.&lt;jsp:forward&gt;</h2><p>页面重定向, 停止当前JSP页面的执行, 将客户端请求转交给另一个JSP页面<br><br>注意: 重定向不是转发, 转发是由服务器进行的, 不会引起客户端的二次请求, 浏览器的地址栏不会发生任何变化, 效率要比重定向高<br></p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:forward page=<span class="string">"转向页面的URL地址"</span> /&gt;</span><br></pre></td></tr></table></figure>
<p>只有一个page属性, 用来指向转向页面的URL地址<br><br>注意: 跳转后原JSP页面后边的代码不再被执行<br></p>
<h2 id="3-lt-jsp-param-gt"><a href="#3-lt-jsp-param-gt" class="headerlink" title="3.&lt;jsp:param&gt;"></a>3.&lt;jsp:param&gt;</h2><p>提供参数, 以”name-value”对的形式为别的动作元素提供参数, 一般情况下和<a href="jsp:include">jsp:include</a><a href="jsp:forward">jsp:forward</a><a href="jsp:plugin">jsp:plugin</a>联合使用</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:param name=<span class="string">"参数名字"</span> value=<span class="string">"参数值"</span> &gt;</span><br></pre></td></tr></table></figure>
<p><strong>&lt;jsp:param&gt; + &lt;jsp:include&gt; :</strong><br><br>在加载文件的同时, 向该文件提供信息<br></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:include page=<span class="string">"*.jsp"</span>&gt;</span><br><span class="line">    &lt;jsp:param name=<span class="string">"参数名"</span> value=<span class="string">"参数值"</span> /&gt;</span><br><span class="line">&lt;/jsp:include&gt;</span><br></pre></td></tr></table></figure>

<p><strong>&lt;jsp:param&gt; + &lt;jsp:forward&gt; :</strong><br><br>跳转页面的同时, 向转向的页面传递参数<br></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:forward page=<span class="string">"*.jsp"</span>&gt;</span><br><span class="line">    &lt;jsp:param name=<span class="string">"参数名"</span> value=<span class="string">"参数值"</span> /&gt;</span><br><span class="line">&lt;/jsp:forward&gt;</span><br></pre></td></tr></table></figure>

<h2 id="4-lt-jsp-plugin-gt"><a href="#4-lt-jsp-plugin-gt" class="headerlink" title="4.&lt;jsp:plugin&gt;"></a>4.&lt;jsp:plugin&gt;</h2><p>将服务器端的JavaBean或者Applet下载到客户端执行<br></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:plugin </span><br><span class="line">type=<span class="string">"插件类型bean|applet"</span></span><br><span class="line">code=<span class="string">"类名.class"</span></span><br><span class="line">codeBase=<span class="string">"java类所在目录"</span></span><br><span class="line">[name=<span class="string">"Bean或Applet的名称"</span>]</span><br><span class="line">[archive=<span class="string">"插件执行前需要预加载的类的列表"</span>]</span><br><span class="line">[align=<span class="string">"对齐方式"</span>]</span><br><span class="line">[height=<span class="string">"高度"</span>]</span><br><span class="line">[width=<span class="string">"宽度"</span>]</span><br><span class="line">[hspace=<span class="string">"距屏幕左右的距离, 单位像素"</span>]</span><br><span class="line">[vspace=<span class="string">"距屏幕上下的距离, 单位像素"</span>]</span><br><span class="line">[jreversion=<span class="string">"运行所需jre版本"</span>]</span><br><span class="line">[nspluginurl=<span class="string">"网景浏览器用户能用的jre下载地址"</span>]</span><br><span class="line">[iepluginurl=<span class="string">"ie浏览器用户能用的jre下载地址"</span>]</span><br><span class="line">&gt;</span><br><span class="line">[&lt;jsp:params&gt;</span><br><span class="line">    [&lt;jsp:param name=<span class="string">"参数名"</span> value=<span class="string">"参数值|JSP表达式"</span> /&gt;]</span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">&lt;/jsp:params&gt;]</span><br><span class="line">[&lt;jsp:fallback&gt;文本信息&lt;/jsp:fallback&gt;]</span><br><span class="line">&lt;/jsp:plugin&gt;</span><br></pre></td></tr></table></figure>

<h2 id="5-lt-jsp-useBean-gt"><a href="#5-lt-jsp-useBean-gt" class="headerlink" title="5.&lt;jsp:useBean&gt;"></a>5.&lt;jsp:useBean&gt;</h2><p>装载一个将要在JSP页面使用的JavaBean</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:useBean </span><br><span class="line">id=<span class="string">"JavaBean实例名"</span></span><br><span class="line"><span class="class"><span class="keyword">class</span></span>=<span class="string">"JavaBean全限定类名"</span></span><br><span class="line">scope=<span class="string">"JavaBean实例的作用域, 默认当前页面"</span></span><br><span class="line">/&gt;</span><br></pre></td></tr></table></figure>
<p><strong>JavaBean实例的作用域:</strong><br></p>
<ul>
<li>1.page: 当前页面</li>
<li>2.request: 本次请求</li>
<li>3.session: 本次会话内</li>
<li>4.application: 本应用内一直有效</li>
</ul>
<p>&lt;jsp:useBean&gt;动作元素中间通常包含&lt;jsp:setProperty&gt;动作元素, 用来设置Bean的属性值<br></p>
<h2 id="6-lt-jsp-setProperty-gt"><a href="#6-lt-jsp-setProperty-gt" class="headerlink" title="6.&lt;jsp:setProperty&gt;"></a>6.&lt;jsp:setProperty&gt;</h2><p>获取到Bean实例后, 用来设置或者修改Bean中的属性值<br></p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:setProperty name=<span class="string">"Bean实例名"</span> property=<span class="string">"属性名"</span> value=<span class="string">"属性值"</span> /&gt;</span><br></pre></td></tr></table></figure>
<p>&lt;jsp:setProperty&gt;动作元素通过Bean中的setter方法设置一个或多个属性值, 使用&lt;jsp:setProperty&gt;前必须先用&lt;jsp:useBean&gt;声明Bean, 同时实例名字一定也得匹配<br></p>
<h2 id="7-lt-jsp-getProperty-gt"><a href="#7-lt-jsp-getProperty-gt" class="headerlink" title="7.&lt;jsp:getProperty&gt;"></a>7.&lt;jsp:getProperty&gt;</h2><p>提取指定Bean属性的值, 转成字符串后输出<br></p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:getProperty name=<span class="string">"Bean实例名"</span> Property=<span class="string">"属性名"</span> /&gt;</span><br></pre></td></tr></table></figure>
<p>使用&lt;jsp:getProperty&gt;前必须先用&lt;jsp:useBean&gt;声明Bean, 同时实例名字一定也得匹配<br></p>
<h2 id="JSP内置对象"><a href="#JSP内置对象" class="headerlink" title="JSP内置对象"></a>JSP内置对象</h2><p>可以直接在JSP页面中使用, 不需要实例化<br><br>JSP2.0 – 9种内置对象<br></p>
<table>
<thead>
<tr>
<th>内置对象</th>
<th>对应的类</th>
<th>作用域</th>
</tr>
</thead>
<tbody><tr>
<td>request</td>
<td>javax.servlet.ServletRequest</td>
<td>一次请求</td>
</tr>
<tr>
<td>response</td>
<td>javax.servlet.ServletResponse</td>
<td>当前页面</td>
</tr>
<tr>
<td>pageContext</td>
<td>javax.servlet.jsp.pageContext</td>
<td>一次会话</td>
</tr>
<tr>
<td>session</td>
<td>javax.servlet.http.HttpSession</td>
<td>当前页面</td>
</tr>
<tr>
<td>application</td>
<td>javax.servlet.ServletResponse</td>
<td>整个Web应用</td>
</tr>
<tr>
<td>out</td>
<td>javax.servlet.jsp.JspWriter</td>
<td>当前页面</td>
</tr>
<tr>
<td>config</td>
<td>javax.servlet.ServletConfig</td>
<td>当前页面</td>
</tr>
<tr>
<td>page</td>
<td>java.lang.Object</td>
<td>当前页面</td>
</tr>
<tr>
<td>exception</td>
<td>java.lang.Throwable</td>
<td>当前页面</td>
</tr>
</tbody></table>
<h2 id="1-request"><a href="#1-request" class="headerlink" title="1.request"></a>1.request</h2><p>获取客户端信息<br><br>JSP容器将客户端请求信息封装在request对象中<br><br>客户端发出请求时, 创建request对象<br><br>客户端请求结束后, 销毁request对象<br><br><strong>request常用方法:</strong><br></p>
<ul>
<li>属性:<br><ul>
<li>获取指定属性的属性值 getAttribute(String name)</li>
<li>设置属性名和对应属性值 setAttribute(String name, Object value)</li>
</ul>
</li>
<li>参数:<ul>
<li>获取请求参数的参数值 getParameter(String name)</li>
<li>获取所有请求参数的名字集合 getParameterNames()</li>
<li>获取请求参数的参数值 getParameterValues(String name)</li>
<li>获取所有请求参数名字和值组成的映射对象 getParameterMap()</li>
</ul>
</li>
<li>设定编码格式: setCharacterEncoding(String encoding)</li>
</ul>
<h2 id="2-response对象"><a href="#2-response对象" class="headerlink" title="2.response对象"></a>2.response对象</h2><p>包含从JSP页面返回客户端的所有信息<br><br>常用于设置HTTP标题, 添加cookie, 设置响应内容的类型和状态, 发送HTTP重定向和编码URL<br><br><strong>response对象常用方法</strong></p>
<ul>
<li>添加cookie对象, 在客户端保存cookie信息 addCookie(Cookie cookie)<br></li>
<li>添加HTTP头信息 addHeader(String name, String value)<br></li>
<li>判断指定的HTTP文件头是否存在 containsHeader(String name)<br></li>
<li>向客户端发送错误的状态码 sendError(int)</li>
<li>重定向JSP文件 sendRedirect(String url)</li>
<li>设置MIME类型与编码方式 setContentType(String contentType)</li>
</ul>
<p>注意: sendRedirect()方法可以跳转到任何一个地址的页面, 而&lt;jsp:forward&gt;之内在站内跳转<br></p>
<h2 id="3-out对象"><a href="#3-out对象" class="headerlink" title="3.out对象"></a>3.out对象</h2><p>缓冲的输出流, 向客户端返回信息<br><br><strong>out对象常用方法:</strong><br></p>
<ul>
<li>清除缓冲区内容, 不把数据输出到客户端 clear()</li>
<li>清除缓冲区内容, 同时把数据输出到客户端 clearBuffer()</li>
<li>关闭缓冲区, 并输出缓冲区的数据 close()</li>
<li>输出缓冲区的数据 flush()</li>
<li>获取缓冲区的大小 getBufferSize()</li>
<li>获取剩余缓冲区的大小 getRemaining()</li>
<li>缓冲区是否进行自动清除 isAutoFlush()</li>
<li>输出一个换行符 newLine()</li>
<li>向客户端输出数据 print()</li>
<li>向客户端输出数据并换行 println()</li>
</ul>
<p>典型应用: out.print 向客户端输出数据<br></p>
<h2 id="4-session对象"><a href="#4-session对象" class="headerlink" title="4.session对象"></a>4.session对象</h2><p>记录客户端的访问状态<br><br>会话: 客户打开浏览器与服务器建立连接, 一直到客户关闭浏览器服务器断开连接的的过程<br><br>客户端访问在服务器的多个页面之间反复连接, 有了session, 服务器就知道这些操作都来自于同一个客户端<br></p>
<p><strong>session对象常用方法:</strong><br></p>
<ul>
<li>getAttribute(String name) 获取session范围内指定属性的属性值</li>
<li>setAttribute(String name, Object value) 设置session范围内属性和对应值, 并存储在session对象中</li>
<li>removeAttribute(String name) 删除session范围内指定属性的值</li>
<li>getAttributeNames() 获取所有session对象中存放的属性名</li>
<li>getCreationTime() 返回session被创建的时间</li>
<li>getId() 返回session创建时JSP容器设定的唯一标识</li>
<li>getLastAccessedTime() 返回用户最后一次通过session发送请求的时间, 单位:毫秒</li>
<li>getMaxInactiveInterval() 返回session的失效时间, 两次请求间隔多长时间session就被取消, 单位:秒</li>
<li>isNew() 判断是否为新的session</li>
<li>invalidate() 清空session的内容</li>
</ul>
<h2 id="5-application对象"><a href="#5-application对象" class="headerlink" title="5.application对象"></a>5.application对象</h2><p>获取和设置Servlet的相关信息<br><br>生命周期: 服务器启动到服务器关闭<br><br>一旦application对象创建, 直到服务器关闭一直存在<br><br>封装了JSP所在的Web应用中的信息<br><br><strong>application对象常用方法:</strong><br></p>
<ul>
<li>setAttribute(String name, Object value) 键值对形式存放属性名和值在application对象中</li>
<li>getAttribute(String name) 根据属性名获取属性值</li>
</ul>
<h2 id="6-pageContext对象"><a href="#6-pageContext对象" class="headerlink" title="6.pageContext对象"></a>6.pageContext对象</h2><p>设置page范围内的属性, 设置其他范围内的属性, 访问本页面中的所有其他对象<br><br><strong>pageContext对象常用方法:</strong><br></p>
<ul>
<li>getRequest() 获取当前页request对象</li>
<li>getResponse() 获取当前页response对象</li>
<li>getSession() 获取当前页中的session对象</li>
<li>getServletContext() 获取当前页中的application对象</li>
<li>getServletConfig() 获取当前页中的config对象</li>
<li>getPage() 获取当前页中的page对象</li>
<li>getOut() 获取当前页中的out对象</li>
<li>getException() 返回当前页中的exception对象</li>
<li>getAttribute(String name) 获取page范围内的name属性值</li>
<li>getAttribute(String name, int scope) 获取指定范围内的name属性值<ul>
<li>PageContext.PAGE_SCOPE: page范围</li>
<li>PageContext.REQUEST_SCOPE: request范围</li>
<li>PageContext.SESSION_SCOPE: session范围</li>
<li>PageContext.APPLICATION_SCOPE: application范围</li>
</ul>
</li>
<li>getAttributeNamesInScope(int scope) 获取指定范围内的所有属性名</li>
<li>getAttributeScope(String name) 返回属性name的作用域</li>
<li>setAttribute(String name, Object value) 设置page范围内的name属性</li>
<li>setAttribute(String name, Object value, int scope) 设置指定范围内的name属性</li>
<li>findAttribute(String name) 寻找name属性并返回属性, 找不到返回null</li>
<li>removeAttribute(String name) 删除属性名为name的属性</li>
<li>removeAttribute(String name, int scope) 删除指定的作用域中属性名为name的属性</li>
</ul>
<h2 id="7-page对象"><a href="#7-page对象" class="headerlink" title="7.page对象"></a>7.page对象</h2><p>当前JSP页面本身<br><br>开发中不经常使用<br><br><strong>page对象常用方法</strong><br></p>
<ul>
<li>getClass() 返回当前Object的类</li>
<li>hashCode() 返回当前Object的哈希码</li>
<li>toString() 将Object对象转换成字符串</li>
<li>equals(Object ob) 比较此Object对象是否与指定的Object对象相等</li>
<li>copy(Object ob) 将此Object对象复制到指定的Object对象中</li>
</ul>
<h2 id="8-config对象"><a href="#8-config对象" class="headerlink" title="8.config对象"></a>8.config对象</h2><p>ServletConfig类的一个实例, Servlet初始化时, 通过config对象向Servlet传递信息<br><br>传递信息: 属性键值对, 服务器相关信息<br><br>一般在编写Servlet时, 需要重载Servlet的init()方法时才用到config对象<br></p>
<p><strong>config对象常用方法:</strong><br></p>
<ul>
<li>getServletName() 获取Servlet名称</li>
<li>getServletContext() 获取一个包含服务器信息的ServletContext对象</li>
<li>getInitParameter(String name) 获取Servlet初始化参数的值</li>
<li>getInitParameterName() 获取Servlet初始化所需要的所有参数名, 返回枚举类型</li>
</ul>
<h2 id="9-exception对象"><a href="#9-exception对象" class="headerlink" title="9.exception对象"></a>9.exception对象</h2><p>处理页面错误和异常<br><br>一般做法: 在页面中使用page指令的errorPage属性, 让该属性指向一个专门处理异常的页面<br></p>
<ul>
<li>在JSP页面中有<strong>未捕获</strong>的异常, 生成exception对象, 然后将该exception对象传送到page指令的异常处理页面, 对exception对象进行处理<br></li>
<li>需要page指令的isErrorPage属性设置为true才可以使用exception对象<br></li>
</ul>
<p><strong>exception对象常用方法:</strong><br></p>
<ul>
<li>getMessage() 返回exception对象的异常信息</li>
<li>getLocalizedMessage() 返回本地化语言的异常信息</li>
<li>printStackTrace() 打印异常的栈反向跟踪轨迹</li>
<li>toString() 返回关于异常的简单描述</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/JSP/" rel="tag"><i class="fa fa-tag"></i> JSP</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/07/15/Java%E4%B8%AD%E7%9A%84protected%E4%BF%AE%E9%A5%B0%E8%AF%8D/" rel="prev" title="Java中的protected修饰词">
      <i class="fa fa-chevron-left"></i> Java中的protected修饰词
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/07/18/Java%E4%B8%AD%E7%9A%84%E6%96%B9%E6%B3%95%E9%87%8D%E8%BD%BD/" rel="next" title="Java中的方法重载">
      Java中的方法重载 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fas fa-yin-yang"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">张雪峰</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/muse.js"></script>


<script src="/blog/js/next-boot.js"></script>


  















  

  

</body>
</html>
